import { Prr } from "@/ecjrts/mode";
import { ModsInstance } from "@/func/createMod";
import { useModule } from "../createMod";
import { GetSomeTypeKey } from "@/ecjrts/tools/modHelper";


export interface PrrPrintFnOpt<T> {
    mod?: T;
}

export type PrrMod = GetSomeTypeKey<Required<ModsInstance>, Prr>;


export const PrintRawData = async function (param: PrrPrintFnOpt<PrrMod>, strData: string) {
    const io = useModule();
    let { mod: modName = "prr" } = param;
    let ctrl = io[modName];

    if (!ctrl) {
        throw new Error(`没有${modName}模块`);
    }

    let state = await ctrl.GetState();
    // console.log(state);
    if (state.Result === "OK") {
        if (state.StDeviceStatus == "NODEVICE") {
            Trace(EC_INFO, "-> PrintRawData: no device ");
            return "NoDevice";
        }
        let ret = await ctrl.PrintRawData(strData);
        switch (ret.ret) {
            case 'PrintComplete':
                await EjectPaper({});
                return ret.ret
                break;
            case 'InvalidPrintData':
                return ret.ret
                break;
            case 'DeviceError':
                return ret.ret
                break;
            case 'FatalError':
                return ret.ret
                break;
            default:
                return ret.ret
                break;
        }
    }
}

export const EjectPaper = async function (param: PrrPrintFnOpt<PrrMod>) {
    const io = useModule();
    let { mod: modName = "prr" } = param;
    let ctrl = io[modName];
    if (!ctrl) {
        throw new Error(`没有${modName}模块`);
    }

    let state = await ctrl.GetState();
    if (state.Result === "OK") {
        if (state.StDeviceStatus == "NODEVICE") {
            Trace(EC_INFO, "-> PrintRawData: no device ");
            return "NoDevice";
        }
        let ret = await ctrl.Eject(-1);
        switch (ret.ret) {
            case 'PrintEjected':
                return ret.ret
                break;
            case 'PrintTaken':
                return ret.ret
                break;
            case 'Timeout':
                // RetainPaper({}, router);
                return ret.ret
                break;
            case 'DeviceError':
                return ret.ret
                break;
            case 'FatalError':
                return ret.ret
                break;
            default:
                return ret.ret
                break;
        }
    }
}
